﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;

namespace RegexTesting
{
    public partial class mainWnd : Form
    {
        public mainWnd()
        {
            InitializeComponent();

            try
            {
                using (TextReader tr = new StreamReader(".\\input.txt"))
                {
                    inputTxtbx.Text = tr.ReadToEnd();
                    tr.Close();
                }
                using (TextReader tr = new StreamReader(".\\regex.txt"))
                {
                    regexTxtbx.Text = tr.ReadToEnd();
                    tr.Close();
                }
                using (TextReader tr = new StreamReader(".\\output.txt"))
                {
                    outputTxtbx.Text = tr.ReadToEnd();
                    tr.Close();
                }
            }
            catch
            {
            }


            this.FormClosing += new FormClosingEventHandler(mainWnd_FormClosing);
        }

        void mainWnd_FormClosing(object sender, FormClosingEventArgs e)
        {
            using (TextWriter tw = new StreamWriter(".\\input.txt"))
            {
                tw.Write(inputTxtbx.Text);
                tw.Close();
            }
            using (TextWriter tw = new StreamWriter(".\\regex.txt"))
            {

                tw.Write(regexTxtbx.Text);
                tw.Close();
            }
            using (TextWriter tw = new StreamWriter(".\\output.txt"))
            {
                tw.Write(outputTxtbx.Text);
                tw.Close();
            }
        }
        
        private void splitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string[] list = Regex.Split(inputTxtbx.Text, regexTxtbx.Text);
                
                outputTxtbx.Clear();
                
                for (uint i = 0; i < list.Length; ++i)
                {
                    outputTxtbx.Text += (i + ")");
                    outputTxtbx.Text += list[i];
                    if (i + 1 != list.Length)
                        outputTxtbx.Text += Environment.NewLine;
                }
            }
            catch
            {
                outputTxtbx.Text = "Error in regular expression";
            }
        }

        private void matchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                outputTxtbx.Text = Regex.Match(inputTxtbx.Text, regexTxtbx.Text).Success == true ? "Yes" : "No";
            }
            catch
            {
                outputTxtbx.Text = "Error in regular expression";
            }
        }

        private void replaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                outputTxtbx.Text = Regex.Replace(inputTxtbx.Text, regexTxtbx.Text, "");
            }
            catch
            {
                outputTxtbx.Text = "Error in regular expression";
            }
        }

        private void saveRegexToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.InitialDirectory = "c:\\";
            dlg.Filter = "Regex Files |*.regex";
            dlg.Title = "Select a regex file";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                TextWriter tr = new StreamWriter(dlg.FileName);
                tr.Write(regexTxtbx.Text);
                tr.Close();
            }
        }

        private void openRegexToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = "c:\\";
            dlg.Filter = "Regex Files |*.regex";
            dlg.Title = "Select a regex file";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                TextReader tr = new StreamReader(dlg.FileName);
                regexTxtbx.Text = tr.ReadToEnd();
                tr.Close();
            }
        }
        
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Regex Testing\n\nanthonyeden0@gmail.com");
        }


    }
}
